<template>
    <div>
        <Form :label-width="100" inline>
            <Form-item label="区/县">
                <dictSelect :sel_val='queryParam.ssfc0001' dicType="service"
                            dictService="/api/ac/subsistence/extSysDictService/getSs01001List" clearable
                            :bindData.sync="queryParam.ssfc0001" style="width:100px"/>
            </Form-item>
            <Form-item label="发放年月">
                <DatePicker type="month" v-model="queryParam.ssfc0002" style="width: 100px"
                            format="yyyy-MM"  :editable="false"></DatePicker>
            </Form-item>
            <Form-item label="救助对象类别">
                <dictSelect :sel_val='queryParam.ssfc0004' dictCode="assistancetype"
                            :bindData.sync="queryParam.ssfc0004" style="width: 200px" clearable/>
            </Form-item>
            <Form-item label="生成是否成功">
                <dictSelect :sel_val='queryParam.ssfc0008' dictCode="yesorno" :bindData.sync="queryParam.ssfc0008"
                            style="width: 80px" clearable/>
            </Form-item>
            <Form-item label="查验状态">
                <dictSelect :sel_val='queryParam.ssfc0015' dictCode="ssfc0015"
                            :bindData.sync="queryParam.ssfc0015" style="width: 150px" clearable/>
            </Form-item>
            <Form-item label="是否确认">
                <dictSelect :sel_val='queryParam.ssfc0010' dictCode="yesorno" :bindData.sync="queryParam.ssfc0010"
                            style="width: 80px" clearable/>
            </Form-item>
            <Row class="button_row">
                <span style="float: right;">
                   <Button type="primary" @click="searchQuery18900">查询</Button>
                <Button type="primary" class="button_item_left" @click="searchReset">重置</Button>
               </span>
            </Row>
        </Form>
        <Modal v-model="visible" :title="title" width="600" style="position:relative;">
            <Form ref="formData" :model="formData" :label-width="150" :rules="rules" :disabled="disableSubmit">
                <FormItem label="发放年月" prop="ssfc0002">
                    <DatePicker type="month" v-model="formData.ssfc0002" style="width: 200px"
                                format="yyyy-MM"  :editable="false"></DatePicker>
                </FormItem>
                <FormItem label="救助对象类别" prop="ssfc0004">
                    <dictSelect dictCode="assistancetype" :bindData.sync="formData.ssfc0004"
                                :sel_val="formData.ssfc0004" style="width: 200px"/>
                </FormItem>
                <FormItem label="备注">
                    <Input v-model="formData.remark" maxlength="100" show-word-limit type="textarea" placeholder="请输入备注"
                           style="width: 200px"/>
                </FormItem>
            </Form>
            <div slot="footer">
                <Button type="text" size="large" @click="visible = false">取消</Button>
                <template v-if="!disableSubmit">
                    <Button type="primary" size="large" @click="handleSubmit('formData')">确定</Button>
                </template>
            </div>
        </Modal>
        <Modal v-model="visibleForCheck" title="救助对象花名册查验" width="800" style="position:relative;">
            <Form ref="formData"  :label-width="150" :rules="rules"  >
                <FormItem label="功能说明：" prop="">
                    <font color="red">
                        根据沈阳市民政局保障救助对象最新管理流程，月度保障花名册确认前需进行本市公安户籍查验，筛查是否存在异常状态保障户，如果存在则必须处理后才能进行花名册确认。<br>
                        1、本功能选定花名册数据查验时间，系统会在选定日期凌晨自动进行数据查验并在当天反馈结果。<br>
                        2、查验数据有时效性，查验通过尽快完成花名册确认，以防数据失效造成无法确认的问题。<br>
                        3、重新生成花名册后原查验计划失效，需重新设定查验计划。
                    </font>
                </FormItem>
                <FormItem label="计划查验时间：" prop="ssfc0014">
                    <DatePicker  v-model="ssfc0014" style="width: 200px"
                                 format="yyyy-MM-dd"  :editable="false"></DatePicker>
                </FormItem>
            </Form>
            <div slot="footer">
                <Button type="text" size="large" @click="visible = false">取消</Button>
                <Button type="primary" size="large" @click="handleCheckSubmit()">确定</Button>
            </div>
        </Modal>

        <div class="button_row">
            <Table :columns="columns" :data="tableData" :loading="Loading"  >
                <template slot-scope="{ row, record, index  }" slot="action">
                    <Button type="success" size="small" style="margin-right: 5px" @click="handleDetail(index)">详情
                    </Button>
                </template>
            </Table>
            <Page :total="dataCount" :page-size="pageSize" show-total class="paging"
                  @on-change="handleTableChange" :current="pageNo"></Page>
        </div>
    </div>
</template>

<script>
    import {postAction} from '@/api/request'
    import {CommonMixin} from '@/views/common/js/CommonMixin'
    import dictSelect from '@/components/dictSelect/dictSelect.vue'
    import DateFormat from '@/plugins/format.js'

    export default {
        mixins: [CommonMixin],
        components: {
            dictSelect,
        },
        data() {
            return {
                options: [],
                rules: {
                    ssfc0002: [
                        {required: true, message: '请选择"发放年月"', trigger: 'blur', type: 'date'}
                    ],
                    ssfc0004: [
                        {required: true, message: '请选择"救助对象类别"', trigger: 'blur'}
                    ]
                },
                columns: [
                    {
                        title: '发放年月',
                        align: 'center',
                        key: 'ssfc0002',
                        render: (h, params) => {
                            let value = params.row.ssfc0002
                            if (typeof (value) === 'undefined' || value === null || value === '' || value === undefined) {
                                value = '--'
                            } else {
                                value = DateFormat(value, 'YYYY-MM')
                            }
                            return h('span', value + ' ')
                        },
                        width: 100,
                        fixed: 'left'
                    },
                    {
                        title: '区县名称',
                        align: 'center',
                        key: 'azaa0002',
                        width: 100,
                        fixed: 'left'
                    },
                    {
                        title: '救助对象类别',
                        align: 'center',
                        key: 'ssfc0004_name',
                        width: 200,
                        fixed: 'left'
                    },
                    {
                        title: '是否生成',
                        align: 'center',
                        key: 'ssfc0005_name',
                        width: 100,
                    },
                    {
                        title: '生成时间',
                        align: 'center',
                        key: 'ssfc0006',
                        width: 120,
                        render: (h, params) => {
                            let value = params.row.ssfc0006
                            if (typeof (value) === 'undefined' || value === null || value === '' || value === undefined) {
                                value = '--'
                            } else {
                                value = DateFormat(value, 'YYYY-MM-DD')
                            }
                            return h('span', value + ' ')
                        },
                    },
                    {
                        title: '生成人姓名',
                        align: 'center',
                        key: 'ssfc0007',
                        width: 120,
                    },
                    {
                        title: '生成是否成功',
                        align: 'center',
                        key: 'ssfc0008_name',
                        width: 120
                    },
                    {
                        title: '查验申请人',
                        align: 'center',
                        key: 'ssfc0013',
                        width: 120,
                    },
                    {
                        title: '查验计划时间',
                        align: 'center',
                        key: 'ssfc0014',
                        width: 120,
                        render: (h, params) => {
                            let value = params.row.ssfc0014
                            if (typeof (value) === 'undefined' || value === null || value === '' || value === undefined) {
                                value = '--'
                            } else {
                                value = DateFormat(value, 'YYYY-MM-DD')
                            }
                            return h('span', value + ' ')
                        },
                    },
                    {
                        title: '查验状态',
                        align: 'center',
                        key: 'ssfc0015_name',
                        width: 100,
                    },
                    {
                        title: '是否确认',
                        align: 'center',
                        key: 'ssfc0010_name',
                        width: 100,
                    },
                    {
                        title: '确认时间',
                        align: 'center',
                        key: 'ssfc0012',
                        width: 100,
                        render: (h, params) => {
                            let value = params.row.ssfc0012
                            if (typeof (value) === 'undefined' || value === null || value === '' || value === undefined) {
                                value = '--'
                            } else {
                                value = DateFormat(value, 'YYYY-MM-DD')
                            }
                            return h('span', value + ' ')
                        },
                    },
                    {
                        title: '确认人',
                        align: 'center',
                        key: 'ssfc0011',
                        width: 100,
                    },
                    // {
                    //     title: '操作',
                    //     key: 'action',
                    //     align: 'center',
                    //     slot: 'action',
                    // }
                ],
                tableData: [],
                Loading: false,

                url: {
                    list: "/api/ac/subsistence/ss18900Service/getPageList",
                    delete: "/api/ac/subsistence/ss18900Service/deleteData",
                    generate:"/api/ac/subsistence/ss18900Service/updateAndCreateRosterData",
                },
                formData: {},
                queryParam: {},
                fieldsToValidate: ["ssfc0002", "ssfc0004"],
                results: {},
                visible: false,
                different: "",
                dataCount: 0,
                pageSize: 10,
                modal: false,
                title: "",
                disableSubmit: false,
                dataNum: 0,
                selectedData: '',
                visibleForCheck : false,
                ssfc0014:'',
                ss18900Id:'',
            }
        },
        methods: {
        searchQuery18900(){
            let that = this;
            that.queryParam.ssfc0002 = DateFormat(that.queryParam.ssfc0002, 'YYYY-MM-DD');
            this.loadData();
        },
            rosterExe(){
                var dataParam = {
                }
                postAction('/api/ac/subsistence/ss00005Service/createSs18900BetaCheck', dataParam).then((res) => {
                })
            },
            //花名册查验
            rostercheck(){
                if (this.dataNum != 1){
                    this.$Message.error('请选中一条数据!')
                    return false;
                }else {
                    let that = this;
                    let record = that.selectedData;
                    if(record.ssfc0005 != '1' ||record.ssfc0008 != '1' ){
                        this.$Message.error("请生成救助花名册，再制定查验计划！");
                        return;
                    }else if(record.ssfc0010 == '1'){
                        this.$Message.error("已经确认救助花名册，不允许再次查验！");
                        return;
                    }else if(record.ssfc0015 == '1'){

                        this.$Modal.confirm({
                            title: '已经通过公安户籍查验的救助对象花名册，确定重新进行查验?',
                            onOk: () => {
                                that.doCalculate();
                            }
                        })
                    }else{
                        this.ss18900Id = record.id;
                        this.visibleForCheck = true;
                        // var url = "ss18900Controller.do?goCheck&id="+row.id;
                        // add('花名册公安户籍查验', url, "ss18900List", 650, 200);
                    }
                }
            },
            //花名册查验计划
            handleCheckSubmit(){
                if(this.ssfc0014 =='' || this.ssfc0014== undefined) {
                    this.$Message.error("请录入计划查验时间！");
                } else {
                    var dataParam = {
                        ss189001Id: this.ss18900Id,
                        ssfc0014: DateFormat(this.ssfc0014, 'YYYY-MM-DD')
                    }
                    postAction('/api/ac/subsistence/ss18900Service/updateAndMakeCheckPlan', dataParam).then((res) => {
                        this.$Message.success("制定查验计划成功！");
                        this.visibleForCheck = false;
                        this.loadData();
                    })
                }
            },
          generateRoster () {
            let that = this
            if (this.dataNum != 1) {
              that.$Message.error('请选择要生成救助花名册的数据！')
              return false
            } else {
              let row=that.selectedData;
              let url=that.url.generate;
              let msg='确定生成此数据的救助对象花名册吗？';
              let ssfc0005=row.ssfc0005;
              if("1"==ssfc0005){
                msg='已经生成此数据的救助对象花名册，确定重新生成吗？';
              }
              let dataParam={};
              dataParam.id=row.id;
              that.$Modal.confirm({
                title: msg,
                onOk: () => {
                  postAction(url, dataParam).then((res) => {
                    if (res.data.errcode != 0) {
                      this.$Message.error(res.data.errmsg)
                    } else {
                      that.loadData();
                      that.$Message.success(res.data.data.msg);
                    }
                  }).finally(() => {
                    that.Loading = false
                  })
                },
                onCancel: () => {
                }
              });
            }

          },
            select(selection, row) {
                this.dataNum = selection.length;
                this.selectedData = row;
            },
            handleAdd() {
                this.$refs['formData'].resetFields();
                let that = this;
                that.title = "花名册录入";
                that.disableSubmit = false;
                let record = {};
                that.visible = true;
                that.different = that.uuid();
                that.formData = Object.assign({}, record)
                that.formData.ssfc0002 = undefined;
            },
            handleDetail(index) {
                let that = this;
                that.title = "详情";
                that.disableSubmit = true;
                let record = that.tableData[index];
                if (record.id) {
                    that.visible = true;
                    that.different = that.uuid();
                    that.formData = Object.assign({}, record);
                } else {
                    that.visible = false
                }
            },
            handleEdit(index) {
                let that = this;
                that.title = "花名册编辑";
                that.disableSubmit = false;
                let record = that.tableData[index];
                if (record.id) {
                    that.visible = true;
                    that.different = that.uuid();
                    that.formData = Object.assign({}, record);
                    that.dataSs11004.ssfc0002 = new Date(record.ssfc0002);
                } else {
                    that.visible = false
                }
            },
            handleSubmit(name) {
                let that = this;
                let validate = true;
                let err_msg = [];
                for (let item of that.fieldsToValidate) {
                    that.$refs[name].validateField(item, (error) => {
                        if (error) {
                            validate = false;
                            err_msg.push(that.rules[item][0].message);
                        }
                    })
                }
                if (validate) {
                    let dataParam = {
                        id: that.formData.id,
                        ssfc0002: DateFormat(that.formData.ssfc0002, 'YYYY-MM-DD'),
                        ssfc0004: that.formData.ssfc0004,
                        remark: that.formData.remark
                    }
                    that.Loading = true;
                    let url = "/api/ac/subsistence/ss18900Service/createData";
                    if (this.formData.id) {
                        url = "/api/ac/subsistence/ss18900Service/updateData";
                    }
                    postAction(url, dataParam).then((res) => {
                        if (res.data.errcode != 0) {
                            this.$Message.error(res.data.errmsg);
                        } else {
                            this.modalFormOk();
                            this.visible = false;
                        }
                    }).finally(() => {
                        that.Loading = false;
                    });
                } else {
                    that.$Message.error(err_msg[0]);
                    return;
                }
            },
            searchYearMonth: function ($event) {
                this.formData.ssfc0002 = $event;
                eventBus.$emit('ssfc0002', this.formData.ssfc0002);
                tableOperationReport.methods.showAll();
            }
        },
        created() {
            this.loadData();
        },
    }
</script>
<style scoped>
    /deep/ .ivu-form-item-error-tip {
        display: none;
    }

    .ivu-form-item {
        margin-bottom: 0px;
    }

    .itemStyle {
        width: 180px
    }

    .head-img1 {
        height: 200px;
        width: 250px;
        margin-top: 15px;
    }

    .head-img {
        height: 200px;
        width: 250px;
    }

    .imgBackColor {
        background-color: WhiteSmoke;
        height: 250px;
    }

    .positiveTDiv {
        height: 240px;
        width: 75px;
        line-height: 240px;
        background-color: #7CCD7C;
        margin-bottom: 10px;
        color: white;
        font-size: 14px;
        font-weight: 700;
    }
</style>
